package org.car.world.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.car.world.common.util.Page;
import org.car.world.core.entily.AdminTable;
import org.car.world.core.entily.TypeTable;
import org.car.world.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Administrator
 * @ClassName: TypeController
 * @Description:分类管理
 * @date Nov 6, 2018
 */
@Controller
@RequestMapping("admin")
public class TypeController {


    @Autowired
    TypeService typeservice;

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: init_method
     * @Description: 跳转到页面
     */
    @GetMapping("dotype")
    public String dotype_init() {

        return "type_list";
    }

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dotype_init
     * @Description: 类型管理页面访问初始化
     */
    @GetMapping("datalist")
    @ResponseBody
    public Map<String, Object> dotype_init(@RequestParam(value = "typename", required = false, defaultValue = "") String typename
            , @RequestParam(value = "form", required = false, defaultValue = "1") Integer form) {
        Map<String, Object> mo = new HashMap<String, Object>(2);
        Page<TypeTable> Pagedata = new Page<TypeTable>();
        Pagedata.setCurrentPage(form);
        Pagedata.setPageSize(2);
        Pagedata.setTotalRow(typeservice.selectconent());
        Pagedata.setPageList(typeservice.selectlimit(Pagedata.getCurrentPage(), Pagedata.getPageSize(), typename));
        mo.put("Page", Pagedata);
        return mo;
    }


    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dotype_init
     * @Description: 访问查看信息的页面
     */
    @GetMapping("datainfo")
    public String dotype_info(@RequestParam(value = "tid") Integer tid, Model mo) {
        mo.addAttribute("TypeTable", typeservice.selectByPrimaryKey(tid));
        return "type_info";
    }

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dotype_init
     * @Description: 添加分类
     */
    @GetMapping("dotypeadd")
    public String dotypeadd_init() {
        return "type_add";
    }


    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dotype_init
     * @Description: 添加分类方法
     */
    @GetMapping("dataadd")
    @ResponseBody
    public boolean datatypeadd_method(TypeTable typeTable, HttpSession session) {

        AdminTable adminTable = (AdminTable) session.getAttribute("Admin");
        typeTable.setTypeCreate(adminTable.getAdminId());
        int flag = typeservice.insert(typeTable);
        if (flag > 0) {
            return true;
        }
        return false;
    }


    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dotype_init
     * @Description: 修改分类初始化方法
     */
    @GetMapping("dataup")
    public String dotypeadd_init(Integer tid, Model mo) {

        TypeTable typeTable = typeservice.selectByPrimaryKey(tid);
        if (typeTable != null) {
            mo.addAttribute("TypeTable", typeTable);
            return "type_update";
        } else {
            return "redirect:dotype";
        }
    }

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dotype_init
     * @Description: 更新分类方法
     */
    @GetMapping("dataupdate")
    @ResponseBody
    public boolean datatypeup_method(TypeTable typeTable, HttpSession session) {

        AdminTable adminTable = (AdminTable) session.getAttribute("Admin");
        typeTable.setTypeUpdateuser(adminTable.getAdminId());
        int flag = typeservice.insert(typeTable);
        if (flag > 0) {
            return true;
        }
        return false;
    }

    /**
     * @param @return 参数
     * @return String    返回类型
     * @throws
     * @Title: dotype_init
     * @Description: 删除分类方法
     */
    @GetMapping("datadel")
    @ResponseBody
    public boolean datatypedel_method(Integer tid) {

        int flag = typeservice.deleteByPrimaryKey(tid);
        if (flag > 0) {
            return true;
        }
        return false;
    }


}